package ch14;

import java.util.Random;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.image.*;
import javafx.geometry.*;

public class Cards extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) throws Exception {
        CardPane pane = new CardPane();
        Scene scene = new Scene(pane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

class CardPane extends BorderPane {
    GridPane pane = new GridPane();
    Button button = new Button("random");

    public CardPane() {
        PutCards(1, 2, 3, 4);
        Random random = new Random();
        button.setOnMousePressed(e -> PutCards(random.nextInt(54) + 1, random.nextInt(54) + 1, random.nextInt(54) + 1,
                random.nextInt(54) + 1));
    }

    void PutCards(int a, int b, int c, int d) {
        getChildren().clear();
        ImageView imageView1 = new ImageView(new Image("image/card/" + a + ".png", 72, 96, false, false));
        ImageView imageView2 = new ImageView(new Image("image/card/" + b + ".png", 72, 96, false, false));
        ImageView imageView3 = new ImageView(new Image("image/card/" + c + ".png", 72, 96, false, false));
        ImageView imageView4 = new ImageView(new Image("image/card/" + d + ".png", 72, 96, false, false));
        pane.add(imageView1, 0, 0);
        pane.add(imageView2, 1, 0);
        pane.add(imageView3, 2, 0);
        pane.add(imageView4, 3, 0);
        setCenter(pane);
        setBottom(button);
        setAlignment(button, Pos.BOTTOM_CENTER);
    }
}